<script setup>
import { RouterLink, RouterView } from "vue-router"
import { onMounted } from "vue"
import { useRoute } from "vue-router";
import { getLocation } from "@/api/location";
import { useCounterStore } from "@/stores/counter"

const counterStore = useCounterStore()
const route = useRoute();




const ocpc = () => {
  // oCPC初始化
  let qhclickid = ""  // 360id
  let bd_vid = ""     // 百度id
  // console.log(route.query)
  qhclickid = route.query.qhclickid;
  bd_vid = route.query.bd_vid;

  // baidu 本地初始化
  if (bd_vid) {
    // 写入缓存
    sessionStorage.setItem("bd_vid", bd_vid);
  }

  // 360 本地初始化
  else if (qhclickid) {
    // 写入缓存
    sessionStorage.setItem("qhclickid", qhclickid);
  }

}

const getLocation2 = () => {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(async (position) => {
      // console.log("纬度: " + position.coords.latitude)
      // console.log("经度: " + position.coords.longitude)
      let res = await getLocation({ latitude: position.coords.latitude, longitude: position.coords.longitude })
      console.log(res)
      // 成功后保存数据到本地缓存,首次进入时再重新获取位置信息
      if (res.info == 'OK') {
        sessionStorage.setItem("regeocode", JSON.stringify(res.regeocode));
        // console.log('保存值', res.regeocode);
        // console.log('国家', res.regeocode.country);
        // console.log('省份', res.regeocode.province);
        // console.log('地区', res.regeocode.district);

      }
    })
  }


  else {
    // console.log("该浏览器不支持获取地理位置，使用默认位置（深圳）。") 
  }
}


onMounted(() => {

  // getLocation2()
  // 判断token
  let token = localStorage.getItem('token');
  if (token) {
    // 获取用户信息保存到pinia
    counterStore.getUserDetails();
  }

  setTimeout(() => {
    ocpc();
    if (!Object.keys(route.query).length) {
      setTimeout(() => {
        ocpc()
      }, 2000)
    }
  }, 1000)
})

</script>

<template>
  <n-message-provider>
    <RouterView :key="$route.fullPath">
    </RouterView>
  </n-message-provider>

</template>

<style scoped></style>
